package server;

import utils.ProtocolHeaderUtil;
import utils.StaticResourceUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

public class Response {
    private OutputStream outputStream ;
    public Response(OutputStream outputStream){
        this.outputStream = outputStream;
    }
    public Response(){}

    public OutputStream getOutputStream() {
        return outputStream;
    }

    public void setOutputStream(OutputStream outputStream) {
        this.outputStream = outputStream;
    }

    /**
     * 获取静态资源文件
     * @param path
     */
    public void outputHtml(String path) throws IOException {
        String absoluteResourcePath = StaticResourceUtil.getAbsolutePath(path);
        //输入静态资源
        File file = new File(absoluteResourcePath);
        if(file.exists()&&file.isFile()){
            //读取静态资源，输出静态资源
            StaticResourceUtil.outputStaticResource(new FileInputStream(file),outputStream);
        }else{
            //静态文件不存在，则输出404
            output(ProtocolHeaderUtil.getHeader404());
        }

    }

    public  void output(String content) throws IOException {
        outputStream.write(content.getBytes());
    }




}
